1번 문제. Lambda operator를 이용한 소수 구하기 구현

  • List Comprehension 형태로 구현된 소수 구하기 스크립트를 Lambda Operator를 이용하여 구현하시오.(map, reduce, filter)
  • 한 줄로 구현할 필요는 없습니다. lambda operator를 이용만 하시면 됩니다.

In [6]:
#List Comprehension 형태
MAX = 1000
noprimes = [
    j
    for i in range(2, int(MAX ** 0.5) + 1)
    for j in range(i*2, MAX, i)
]

primes = [
    x for x in range(2, MAX) if x not in noprimes
]
print(primes)


[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]

In [7]:
primes = list(filter(lambda x: x not in noprimes, range(2, MAX)))
print(primes)


[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]

In [10]:
import functools
MAX = 1000

noprimes = list(map(
    lambda i: list(map(
            lambda j: j,
            range(i*2, MAX, i))),
    range(2, int(MAX**0.5) + 1)
    ))

noprimes = functools.reduce(
    lambda x, y: x+y,
    noprimes,
)

primes = list(filter(lambda x: x not in noprimes, range(2, MAX)))
print(primes)


[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]

2번 문제. replace함수의 구현

  • 문자열을 치환하는 Python 내장 함수인 replace를 직접 구현하세요.

In [12]:
"패스트캠퍼스".replace("패스트","Fast")


Out[12]:
'Fast캠퍼스'

In [13]:
"웹프로그래밍 스쿨과 데이터 사이언스 스쿨".replace("스쿨", "SCHOOL")


Out[13]:
'웹프로그래밍 SCHOOL과 데이터 사이언스 SCHOOL'
  • sentence 라는 문자열에서 find_word 를 찾아서 replace_word 로 변환해주는 함수

In [1]:
def word_replace(sentence, find_word, replace_word):
    result = ""
    i = 0
    
    while i < len(sentence):
        is_exist = True
        for j in range(len(find_word)):
            if sentence[i+j] != find_word[j]:
                is_exist = False
                break
        if is_exist:
            result += replace_word
            i += len(find_word)
            
        else:
            result += sentence[i]
            i += 1
    return result

In [2]:
word_replace("슬로슬로우캠퍼스슬로우","슬로우","slow")


Out[2]:
'슬로slow캠퍼스slow'

In [ ]:


In [ ]:


In [ ]:


In [ ]:
#3번 문제...? 모르겠습니다.
def change_weekday(num):
    change_dic = {
        "0": "월요일",
        "1": "화요일",
        "2": "수요일",
        "3": "목요일",
        "4": "금요일",
        "5": "토요일",
        "6": "일요일",
    }
    for key, value in change_dic.items():
        num = num.replace(key, str(value))

    return num

num = int(calendar.weekday(2016, 5, 8))
change_weekday("3")


#4번 문제...? 모르겠습니다.

histogram = input("문자열을 입력하세요: ")

word_count={}
for word in histogram.replace(',','').split():
    if word in word_count:
        word_count[word]+=1
    else:
        word_count[word]=1
    
print(word_count)

#5번 문제
how_much = int(input())
def fibonacci(how_much):
    result = ""
    for i in range(how_much):
        result += how_much
    return result